import BaseVenueProcessor from './BaseVenueProcessor.js';
import { getXiangYangYaYuanVenueSettings } from '../request.js';

/**
 * 向阳雅园校区场地数据处理器
 */
export class XiangYangYaYuanProcessor extends BaseVenueProcessor {
  constructor() {
    super();
    this.venueName = '向阳雅园校区';
  }

  /**
   * 获取向阳雅园校区场地预约设置
   * @param {Object} params - 查询参数 { basicId, merchantId, userId, reserveDate }
   * @returns {Promise} 请求Promise
   */
  async getVenueSettings(params = {}) {
    // 设置默认参数
    const defaultParams = {
      basicId: "403150429",
      merchantId: "10166",
      userId: "",
      reserveDate: "2025-08-13"
    };
    
    // 合并参数
    const requestData = { ...defaultParams, ...params };
    
    return await getXiangYangYaYuanVenueSettings(requestData);
  }

  /**
   * 验证数据是否有效
   * @param {Object} data - 原始场地数据
   * @returns {boolean} 数据是否有效
   */
  validateData(data) {
    return data && 
           data.data && 
           data.data.siteInfoTOs && 
           Array.isArray(data.data.siteInfoTOs) && 
           data.data.timeIntervalTOs && 
           Array.isArray(data.data.timeIntervalTOs);
  }

  /**
   * 按场地名称分组
   * @param {Object} data - 原始场地数据
   * @returns {Map} 场地Map
   */
  groupVenues(data) {
    const venueMap = new Map();
    
    // 遍历场地信息
    data.data.siteInfoTOs.forEach(site => {
      const venueName = site.name;
      if (!venueMap.has(venueName)) {
        venueMap.set(venueName, {
          name: venueName,
          siteId: site.siteId,
          timeSlots: []
        });
      }
    });
    
    return venueMap;
  }

  /**
   * 处理时间槽
   * @param {Map} venueMap - 场地Map
   * @param {Object} data - 原始场地数据
   */
  processTimeSlots(venueMap, data) {
    // 遍历时间间隔信息
    if (data.data.timeIntervalTOs) {
      data.data.timeIntervalTOs.forEach(interval => {
        // 只看records数组最后一个元素
        if (interval.records && interval.records.length > 0) {
          const record = interval.records[interval.records.length - 1];
          
          // 查找对应的场地
          const site = data.data.siteInfoTOs.find(s => s.siteId === record.siteId);
          if (site) {
            const venueName = site.name;
            const venue = venueMap.get(venueName);
            
            if (venue) {
              // 添加时间槽信息
              venue.timeSlots.push({
                time: interval.timeInterval,
                isFree: record.siteLockStatus === 0, // siteLockStatus为0则无预约
                price: record.basePrice,
                beginDatetime: new Date(interval.baseStartTimeStamp).toISOString(),
                endDatetime: new Date(interval.baseEndTimeStamp).toISOString()
              });
            }
          }
        }
      });
    }
  }

  /**
   * 转换为场地列表
   * @param {Map} venueMap - 场地Map
   * @returns {Array} 场地列表
   */
  convertToVenueList(venueMap) {
    return Array.from(venueMap.values()).map((venue, index) => {
      return {
        id: 4000 + index, // 给向阳雅园校区场地分配ID
        name: venue.name,
        address: '向阳雅园校区',
        latitude: 39.9042,
        longitude: 116.4074,
        phone: '010-87654323',
        rating: 4.7,
        price: 0,
        facilities: ['室内场地', '专业教练'],
        image: '',
        distance: 3.5,
        timeSlots: venue.timeSlots,
        dataSource: this.venueName
      };
    });
  }
}

export default XiangYangYaYuanProcessor;